package com.example.myproject.client;

import com.allen_sauer.gwt.dnd.client.PickupDragController;
import com.allen_sauer.gwt.dnd.client.util.WidgetLocation;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Widget;

public class ModuleDragController extends PickupDragController {

	DragModule moduleImage = null;
	WidgetLocation startLocation;
	
	public ModuleDragController(AbsolutePanel boundaryPanel) {
		super(boundaryPanel, false);
	}
	
	public void dragStart() {
		super.dragStart();
		
		Iterable<Widget> mods = this.getSelectedWidgets();
		for (Widget widget : mods) {
			moduleImage = (DragModule)widget;
			startLocation = new WidgetLocation(moduleImage, moduleImage.getParent().getParent());
		}
	}
	
	public void dragEnd() { // TODO: FIX ALL THESE NUMBERS!!
		WidgetLocation location = new WidgetLocation(moduleImage, moduleImage.getParent().getParent());		
		Module mod = moduleImage.getModule();
		int startX = (startLocation.getLeft() / TestGUI.GRID_WIDTH) + 1;
		int startY = 50 - (startLocation.getTop() / TestGUI.GRID_HEIGHT);
		// (x - 1) * 10 // (50 - y) * 17
		int toX = ((location.getLeft()) / TestGUI.GRID_WIDTH) + 1;
		int toY = 50 - ((location.getTop()) / TestGUI.GRID_HEIGHT);
		HabitatLayout currentLayout = TestGUI.getCurrentLayout();
		currentLayout.moveModule(mod, startX, startY, toX, toY);
		super.dragEnd();
		moduleImage.removeFromParent();
	}
	
}
